/*
    Copyright (C) 2021 Huawei Device Co., Ltd.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 */

package com.danikula.videocache.interfacers;

import com.danikula.videocache.ProxyCacheException;

import java.util.List;

/**
 * 分片缓存接口
 */
public abstract class ISliceCache extends Cache {
    /**
     * 多文件多分片缓存读取缓存数据 (仅读缓存的时候)
     *
     * @param buffer 读取byte数组
     * @param offset 读取起点
     * @param length 读取长度
     * @return 读取的数据
     * @throws ProxyCacheException 读取错误信息
     */
    public abstract int readFile(byte[] buffer, long offset, int length) throws ProxyCacheException;

    /**
     * 多文件多分片缓存写入缓存数据
     *
     * @param data   请求的数据
     * @param offset 缓存起点
     * @param length 请求的数据长度
     * @throws ProxyCacheException 错误信息
     */
    public abstract void writeFile(byte[] data, long offset, int length) throws ProxyCacheException;

    /**
     * 读取缓存的时候 缓存内容是否足够
     *
     * @param offset 读取起点
     * @param length 读取长度
     * @return 缓存内容是否足够
     */
    public abstract boolean isCacheEnough(long offset, int length);

    /**
     * 获取网络请求数据起始位置
     *
     * @return 数据起始位置
     * @throws ProxyCacheException 错误信息
     */
    public abstract long getDownLoadStart() throws ProxyCacheException;

    /**
     * 获取需要被合并到当前文件的的碎片文件的起始位置
     *
     * @return 数据起始位置
     * @throws ProxyCacheException 错误信息
     */
    public abstract long getMergeStart() throws ProxyCacheException;

    /**
     * 获取需要被合并到当前文件的的碎片文件的终止位置
     *
     * @return 数据起始位置
     * @throws ProxyCacheException 错误信息
     */
    public abstract long getMergeEnd() throws ProxyCacheException;


    /**
     * 合并碎片文件
     *
     * @throws ProxyCacheException 错误信息
     */
    public abstract void mergeFiles() throws ProxyCacheException;

    /**
     * 根据当前碎片文件刷新碎片文件的合并信息
     *
     * @param offset 合并点
     * @throws ProxyCacheException 错误信息
     */
    public abstract void refreshMergeInfo(long offset);

    /**
     * 获取缓存数据总大小
     *
     * @return 缓存数据总大小
     */
    public abstract long getTotalCache();

    /**
     * 获取缓存数据总大小
     *
     * @return 缓存数据总大小
     */
    public abstract List<long[]> getAllCacheArea();
}
